package increment1;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * The class Task represents a task.
 * 
 * @author Rolf Schmidt
 * @version 1.0
 */
public class Task {

	/**
	 * contains the ID of this task
	 */
	private String id;
	/**
	 * contains the title of this task
	 */
	private String title;
	/**
	 * contains a (short) description of this task
	 */
	private String description;
	/**
	 * contains the duration of this task in hours
	 */
	private int duration;
	/**
	 * contains the description of the deliverable(s) associated with this task
	 */
	private String deliverable;
	/**
	 * contains the deadline of this task
	 */
	private Calendar deadline;
	/**
	 * contains the name of the person associated with this task
	 */
	private String person;

	public Task() {
		super();
		this.id = "none";
		this.title = "none";
		this.description = "none";
		this.duration = 1; // as this the default set in setDuration()
		this.deliverable = "none";
		this.deadline = Calendar.getInstance();
		this.person = "nobody";
	}

	@Override
	public String toString() {
		final String H_LINE = "---------------------------------------";
		StringBuffer sb = new StringBuffer(H_LINE);

		sb.append("\nID: " + id);
		sb.append("\nTitle: " + title); 
		sb.append("\nDescription: " + description); 
		sb.append("\nDuration: " + new Integer(duration).toString());
		sb.append("\nDeliverable: " + deliverable);
		sb.append("\nDeadline: " + formatDate(deadline.getTime())); 
		sb.append("\nPerson: " + person);
		sb.append("\n" + H_LINE + "\n");
		return sb.toString();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Task other = (Task) obj;
		if (deadline == null) {
			if (other.deadline != null)
				return false;
		} else if (!deadline.equals(other.deadline))
			return false;
		if (deliverable == null) {
			if (other.deliverable != null)
				return false;
		} else if (!deliverable.equals(other.deliverable))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (duration != other.duration)
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (person == null) {
			if (other.person != null)
				return false;
		} else if (!person.equals(other.person))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		if (id == null || id.equals("")) {
			this.id = "none";
		}
		else {
			this.id = id;
		}
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		if (title == null || title.equals("")) {
			this.title = "none";
		}
		else {
			this.title = title;
		}
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		if (description == null || description.equals("")) {
			this.description = "none";
		}
		else {
			this.description = description;
		}
	}

	/**
	 * @return the duration
	 */
	public int getDuration() {
		return duration;
	}

	/**
	 * @param duration
	 *            the duration to set
	 */
	public void setDuration(int duration) {
		if (duration <= 0) {
			this.duration = 1;	//every task takes at least one hour (otherwise it won't show up in the Gantt chart
		}
		else {
			this.duration = duration;
		}
	}

	/**
	 * @return the deliverable
	 */
	public String getDeliverable() {
		return deliverable;
	}

	/**
	 * @param deliverable
	 *            the deliverable to set
	 */
	public void setDeliverable(String deliverable) {
		if (deliverable == null || deliverable.equals("")) {
			this.deliverable = "none";
		}
		else {
			this.deliverable = deliverable;
		}
	}

	/**
	 * @return the deadline
	 */
	public Calendar getDeadline() {
		return deadline;
	}

	/**
	 * @param deadline
	 *            the deadline to set
	 */
	public void setDeadline(Calendar deadline) {
		Calendar aDeadline = Calendar.getInstance();
		if (deadline == null) {
			this.deadline = aDeadline;
		}
		else {
			if (deadline.compareTo(aDeadline) < 0)
				this.deadline = aDeadline;
			else
				this.deadline = deadline;
		}
	}

	/**
	 * @return the person
	 */
	public String getPerson() {
		return person;
	}

	/**
	 * @param person
	 *            the person to set
	 */
	public void setPerson(String person) {
		if (person == null || person.equals("")) {
			this.person = "nobody";
		}
		else {
			this.person = person;
		}
	}

	/**
	 * @param date
	 *            the date to format
	 * @return the formatted date string
	 */
	private String formatDate(Date date) {
		if (date == null) {
			return "";
		}
		String DATE_FORMAT = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);

		return sdf.format(date);
	}

}
